$(function () {
    axios.get('http://jx.xuzhixiang.top/ap/api/cart-list.php?id=1', {
        params: {
            id: "133850"
        }
    }).then((ret) => {
        let gwcarr = ret.data.data
        let spa = gwcarr.map(v => `
    <li class="gwc-splist-a" data-od=${v.pid}>
                    <div>
                        <input type="checkbox" class="gwc-quan">
                        <img src="${v.pimg}" alt="">
                        <span>${v.pname}</span>
                    </div>
                    <div>
                        <span class="gwc-sprice-a"><i>￥</i>${v.pprice}</span>
                        <b>
                            <span class="gwc-del-a">-</span>
                            <input type="text" value="${v.pnum}" class="gwc-val-a">
                            <span class="gwc-add-a">+</span>
                        </b>
                        <span class="gwc-sprice-z"><i>￥</i>${v.pprice*v.pnum}</span>
    <span class="gwcDel" data-od=${v.pid} style="text-indent: 1em;cursor: pointer;
                        ">删除</span>
                    </div>

                </li>
    `)
        document.querySelector("#gwc-show").innerHTML = spa.join("")
        // 清空购物车
        $("#gwc-qingkong").click(function () {
            $("#gwc-show li").remove()
        })

        //  全选框
        $(".gwc-xquan").change(function () {
            $(".gwc-quan,.gwc-xquan").prop("checked", $(this).prop("checked"))
            $("#gwc-Xshu").html($(".gwc-quan:checked").length)
            // 结算变亮
            if ($(".gwc-xquan").prop("checked") == true) {
                $(".gwc-jies").css("background", "rgb(255, 217, 0)")
            } else {
                $(".gwc-jies").css("background", "")
            }
        })
        $(".gwc-quan").change(function () {
            if ($(".gwc-quan:checked").length == $(".gwc-quan").length) {
                $(".gwc-xquan").prop("checked", true)
            } else {
                $(".gwc-xquan").prop("checked", false)
            }
            $("#gwc-Xshu").html($(".gwc-quan:checked").length)
            // 结算变亮
            if ($(".gwc-quan:checked").length >= 1) {
                $(".gwc-jies").css({
                    "background": "rgb(255, 217, 0)",
                    "cursor": "pointer"
                })
            } else {
                $(".gwc-jies").css("background", "")
            }
        })
        // 购买数量
        $("#gwc-Sshu").html($("#gwc-show li").length)
        // 商品总价
        function andZong() {
            let turnNum = function (nums) {
                return nums.map(Number);
            }
            let jieSuan = $(".gwc-sprice-z").text()
            jieSuan = jieSuan.split("￥").slice(1)
            jieSuan = turnNum(jieSuan)
            let jieSuanZ = jieSuan.reduce((a, i) => a + i)
            jieSuanZ = jieSuanZ.toFixed(2)
            $(".gwc-zong").text(jieSuanZ)
        }
        andZong()
        // 删除
        $('.gwcDel').each(function (i, ele) {
            let gwcDelId = $('.gwcDel').eq(i).attr("data-od")
            $('.gwcDel').eq(i).click(function () {

                if ($(".gwc-quan:checked").length >= 1) {
                    $("#gwc-Xshu").html($(".gwc-quan:checked").length - 1)
                }
                axios.get('http://jx.xuzhixiang.top/ap/api/cart-delete.php', {
                    params: {
                        uid: "133850",
                        pid: $(this).attr("data-od")
                    }
                }).then((ret) => {
                    console.log(ret.data)
                    $(this).parents(".gwc-splist-a").remove()
                    // 总额
                    andZong()
                    // 
                })
            })
        })
        // 增与减
        $(".gwc-add-a").each(function (i, ele) {
            $(".gwc-add-a").eq(i).click(function () {
                let price = $(this).parent().siblings(".gwc-sprice-a").text();
                price = price.slice(1);
                let n = $(".gwc-val-a").eq(i).val();
                n++
                $(".gwc-val-a").eq(i).val(n)
                $(".gwc-sprice-z").eq(i).html("￥" + price * n)
                let od = $(this).parents(".gwc-splist-a").attr("data-od")
                axios.get('http://jx.xuzhixiang.top/ap/api/cart-update-num.php', {
                    params: {
                        uid: "133850",
                        pid: od,
                        pnum: n
                    }
                }).then((ret) => {
                    console.log(ret.data)
                })
                // 总额
                andZong()
                // 
            })
            $(".gwc-del-a").eq(i).click(function () {
                let price = $(this).parent().siblings(".gwc-sprice-a").text();
                price = price.slice(1);
                let n = $(".gwc-val-a").eq(i).val();
                n--
                if (n <= 1) {
                    n = 1
                }
                $(".gwc-val-a").eq(i).val(n)
                $(".gwc-sprice-z").eq(i).html("￥" + price * n)
                console.log($(".gwc-sprice-z").html(), n);
                let od = $(this).parents(".gwc-splist-a").attr("data-od")
                axios.get('http://jx.xuzhixiang.top/ap/api/cart-update-num.php', {
                    params: {
                        uid: "133850",
                        pid: od,
                        pnum: n
                    }
                }).then((ret) => {
                    console.log(ret.data)
                })
                // 总额
                andZong()
                // 
            })
        })
    })
})